package com.example.qincetest

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import com.example.qincetest.mvvm.first.FirstActivity
import com.example.qincetest.second.SecondActivity
import com.example.qincetest.ui.activity.NetworkActivity
import com.example.qincetest.ui.activity.WebKit2Activity
import com.example.qincetest.ui.activity.WebViewActivity
import com.example.qincetest.ui.disney.DisneyActivity

class MainActivity : BaseActivity() {

    // 注册权限请求回调
    private val requestPermissionLauncher = registerForActivityResult(
        ActivityResultContracts.RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            // 权限被授予
            openNotificationActivity()
        } else {
            // 权限被拒绝
            Toast.makeText(this, "需要通知权限才能展示通知", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MainScreen()
        }
    }

    public fun checkNotificationPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            when {
                ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.POST_NOTIFICATIONS
                ) == PackageManager.PERMISSION_GRANTED -> {
                    // 已经有权限，直接打开通知页面
                    openNotificationActivity()
                }
                shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> {
                    // 用户之前拒绝过权限，显示解释
                    Toast.makeText(this, "需要通知权限来显示通知，请在设置中开启", Toast.LENGTH_LONG).show()
                }
                else -> {
                    // 请求权限
                    requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
                }
            }
        } else {
            // Android 13以下版本不需要动态申请通知权限
            openNotificationActivity()
        }
    }

    private fun openNotificationActivity() {
        val intent = Intent(this, NotificationActivity::class.java)
        startActivity(intent)
    }

    private fun getUiMode(): Int {
        return resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
    }
}

@Composable
fun MainScreen(
    modifier: Modifier = Modifier
) {
    val context = LocalContext.current
    
    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(8.dp),
        horizontalAlignment = Alignment.Start
    ) {
        Button(
            onClick = { 
                context.startActivity(Intent(context, SecondActivity::class.java))
            }
        ) {
            Text("打开")
        }
        
        Button(
            onClick = { 
                context.startActivity(Intent(context, ExpandedActivity::class.java))
            }
        ) {
            Text("打开 Fragment")
        }
        
        Button(
            onClick = {
                if (context is MainActivity) {
                    context.checkNotificationPermission()
                }
            }
        ) {
            Text("打开 Notification")
        }
        
        Button(
            onClick = { 
                context.startActivity(Intent(context, GlideTestActivity::class.java))
            }
        ) {
            Text("打开 Glide测试")
        }
        
        Button(
            onClick = { 
                context.startActivity(Intent(context, NetworkActivity::class.java))
            }
        ) {
            Text("打开网络测试")
        }
        
        Button(
            onClick = { 
                context.startActivity(Intent(context, DisneyActivity::class.java))
            }
        ) {
            Text("迪斯尼")
        }

        Button(
            onClick = {
                context.startActivity(Intent(context, FirstActivity::class.java))
            }
        ) {
            Text("Retrofit")
        }

        Button(
            onClick = {
                context.startActivity(Intent(context, WebViewActivity::class.java))
            }
        ) {
            Text("WebView")
        }
        Button(
            onClick = {
                context.startActivity(Intent(context, WebKit2Activity::class.java))
            }
        ) {
            Text("Webkit")
        }
    }
}

@Preview(
    name = "Light Mode",
    showBackground = true,
    backgroundColor = 0xFFFFFFFF
)
@Preview(
    name = "Dark Mode",
    showBackground = true,
    backgroundColor = 0xFF000000,
    uiMode = Configuration.UI_MODE_NIGHT_YES
)
@Composable
fun MainScreenPreview() {
    MainScreen()
}
